package com.elitesland.metadata.rest;

import com.elitesland.core.base.ApiCode;
import com.elitesland.core.base.ApiResult;
import com.elitesland.core.base.PagingVO;
import com.elitesland.metadata.param.MetaColumnQParam;
import com.elitesland.metadata.param.MetaTableQParam;
import com.elitesland.metadata.param.MetaTableSaveParam;
import com.elitesland.metadata.service.MetaColumnService;
import com.elitesland.metadata.service.MetaTableService;
import com.elitesland.metadata.vo.MetaColumnVO;
import com.elitesland.metadata.vo.MetaTableVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * <pre>作用说明</pre>
 *
 * @author mir
 * @date 12/4/2020 1:30 AM
 */
@RestController
@RequestMapping("/meta/tables")
@Api(value = "元数据数据表管理", tags = {"元数据数据表管理"})
public class MetaTableController {

    private MetaTableService metaTableService;

    private MetaColumnService metaColumnService;

    @Autowired
    public void setMetaTableService(MetaTableService metaTableService) {
        this.metaTableService = metaTableService;
    }

    @Autowired
    public void setMetaColumnService(MetaColumnService metaColumnService) {
        this.metaColumnService = metaColumnService;
    }

    @PostMapping("/q")
    @ApiOperation("检索数据表记录")
    public CompletableFuture<ApiResult<PagingVO<MetaTableVO>>> search(@RequestBody MetaTableQParam param) {
        return metaTableService.search(param).thenApply(ApiResult::ok);
    }

    @GetMapping("/{id}")
    @ApiOperation("通过记录ID获取数据表详情")
    public ApiResult<MetaTableVO> oneById(@PathVariable Long id) {
        return metaTableService.oneById(id).map(ApiResult::ok)
                .orElse(ApiResult.fail(ApiCode.NOT_FOUND));
    }

    @PostMapping("/q/{id}/columns")
    @ApiOperation("检索数据表下的列定义")
    public CompletableFuture<ApiResult<Object>> searchColumns(
            @PathVariable Long id,
            @RequestBody MetaColumnQParam param
    ) {
        return metaColumnService.search(id, param).thenApply(ApiResult::ok);
    }

    @PostMapping
    @ApiOperation("新增数据表定义")
    public ApiResult<Long> create(@RequestBody MetaTableSaveParam table) {
        val id = metaTableService.create(table);
        return ApiResult.ok(id);
    }

    @PutMapping
    @ApiOperation("更改数据表定义")
    public ApiResult<Object> update(@RequestBody MetaTableSaveParam table) {
        metaTableService.update(table);
        return ApiResult.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据记录ID删除数据表定义，及相关列定义")
    public ApiResult<Object> removeById(@PathVariable Long id) {
        metaTableService.removeById(id);
        return ApiResult.ok();
    }

    @PostMapping("/{id}/columns")
    @ApiOperation("批量添加或更新列定义")
    public ApiResult<Object> updateColumns(
            @PathVariable Long id,
            @RequestBody List<MetaColumnVO> columns
    ) {
        metaColumnService.addColumns(id, columns);
        return ApiResult.ok();
    }

    @DeleteMapping("/{id}/columns")
    @ApiOperation("批量移除列定义")
    public ApiResult<Object> removeColumns(
            @PathVariable Long id,
            @RequestBody List<Long> columnIds
    ) {
        metaColumnService.removeColumns(id, columnIds);
        return ApiResult.ok();
    }
}
